package collection;

import groupby.manage.GroupbyManage;
import groupby.model.Groupby;

import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dic.manage.DicManage;
import dic.model.City;

/**
 * 美团网采集程序
 * @author acer
 *
 */
public class MeiTuan {

	public static void startCollection(ApplicationContext context)
	{
		String urlStr="http://www.meituan.com/api/v1/divisions";
		StringBuffer s = new StringBuffer();
		try {
			URL url = new URL(urlStr);
			
				SAXReader reader = new SAXReader();
				Document doc = reader.read(url);
				Element root = doc.getRootElement();
				
				List list = doc.selectNodes("//divisions/division");
				for (Iterator it = list.iterator(); it.hasNext();) {
					Element element=(Element) it.next();
					Node node=element.selectSingleNode("id");
					addData(node.getText(),context);
				}
				
			

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void addData(String city,ApplicationContext context)
	{
		String urlStr="http://www.meituan.com/api/v1/"+city+"/deals";
		StringBuffer s = new StringBuffer();
		GroupbyManage groupbyManage=(GroupbyManage)context.getBean("groupbyManage");
		try {
			URL url = new URL(urlStr);
			
				SAXReader reader = new SAXReader();
				Document doc = reader.read(url);
				Element root = doc.getRootElement();
				List list=doc.selectNodes("//deals/deal");
				for (Iterator it = list.iterator(); it.hasNext();) {
					Element element=(Element) it.next();
					Node title=element.selectSingleNode("title");
					Node dealUrl = element.selectSingleNode("deal_url");
					Node image = element.selectSingleNode("large_image_url");
					Node cityId = element.selectSingleNode("division_name");
					Node startDate=element.selectSingleNode("start_date");
					Node endDate=element.selectSingleNode("end_date");
					Node price=element.selectSingleNode("price");
					Node originalCost=element.selectSingleNode("value");
					Node discountPercent=element.selectSingleNode("discount_percent");
					Node discountAmount=element.selectSingleNode("discount_amount");
					Groupby groupby=new Groupby();
					
					groupby.setTitle(title.getText());
					groupby.setDealUrl(dealUrl.getText());
					groupby.setImage(image.getText());
					
					DicManage dicManage=(DicManage)context.getBean("dicManage");
					if(cityId.getText().equals("全国"))
					{
					   return ;
					}
					City cityObj=dicManage.findCityByName(cityId.getText());
					if(cityObj==null||cityObj.getFlag()!=1)
					{
					    return ;
					}
					boolean isFind=groupbyManage.isFindGroupby(title.getText(),cityObj.getId());
					if(!isFind)
					{
						return ;
					}
					groupby.setCityId(cityObj.getId());
					
					groupby.setStartDate(startDate.getText().substring(0,10)+" "+startDate.getText().substring(11,19));
					groupby.setEndDate(endDate.getText().substring(0,10)+" "+endDate.getText().substring(11,19));
					
					groupby.setPrice(Float.parseFloat(price.getText()));
					groupby.setOriginalCost(Float.parseFloat(originalCost.getText()));
					groupby.setDiscountPercent(discountPercent.getText());
					groupby.setDiscountAmount(Float.parseFloat(discountAmount.getText()));
					SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					groupby.setInputDatetime(sdf.format(new Date()));
					groupby.setTopCount(0);
					groupby.setWebsite("美团网");
					
					groupbyManage.save(groupby);
				}
	

		} catch (Exception e) {
			e.printStackTrace();
		}
	} 
}
